package com.chatRobot;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chatRobot.RequstHttpUtil.NewHttp;
import com.chatRobot.localtionDemo.DataSavedLocally;

import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;

public class maincost {
    private static String url_assistant = ""   ; //回复内容
    private static List<String> arrayList = new ArrayList<>();         //messages 的数据结构    [{},{},{}]

    private static List<String> outlist = new ArrayList<>();

    public static void main(String[] args) {
        File file = new DataSavedLocally().fhwj();
        while (true){
            System.out.println("请输入你的问题：");
            Scanner scanner = new Scanner(System.in);
            String next = scanner.next();
            if(next.equals("exit")){
                System.exit(0);
            }else {
                System.out.println("请稍等，回复内容中.......");
                //执行修改

                url_assistant = postGTP(next);
                new DataSavedLocally().saveQuestionAndAnswer(next,url_assistant,file);
                System.out.println(url_assistant);
            }
        }
    }

    /**
     * 调用接口进行数据访问返回对应的值
     * @param wt
     * @return
     */
    public static String postGTP(String wt) {
        String url = "http://ai.qujiami.cn/api/openai/v1/chat/completions";     //网页：http://ai.qujiami.cn/#/chat
        String post_value = new NewHttp().Post(url, enteredGinseng(wt,url_assistant));  //调用GPT接口
        return adjustingData(post_value);
    }

    /**
     * 调整GPT返回的数据格式
     * @param reverseData
     * @return
     */
    public static String adjustingData(String reverseData){
        String[] split = reverseData.split("data:");
        StringBuilder strac = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            if(s.contains("content")){
                JSONObject jsonObject = JSONObject.parseObject(s);
                JSONArray choicesList = jsonObject.getJSONArray("choices");
                String choiceOne = choicesList.getString(0);
                JSONObject jsonChoiceOne = JSONObject.parseObject(choiceOne);
                String contentValue = jsonChoiceOne.getJSONObject("delta").getString("content");
                strac.append(contentValue);
            }
        }
        return strac.toString();
    }

    /**
     * 调整调用GPT的入参数据格式
     */
    public static String enteredGinseng(String question,String answer){
        Map<String, String> messages_data = new HashMap<>();
        if(!answer.equals("")){
            messages_data.put("role","assistant");
            messages_data.put("content",answer);
            arrayList.add(JSONObject.toJSONString(messages_data));
        }
        messages_data.put("role","user");
        messages_data.put("content",question);
        arrayList.add(JSONObject.toJSONString(messages_data));
        Map<String, Object> hashMap = new HashMap<>();     //主发送数据
        hashMap.put("frequency_penalty", 0);
        hashMap.put("model", "gpt-3.5-turbo-16k");
        hashMap.put("presence_penalty", 0);
        hashMap.put("stream", "true");
        hashMap.put("temperature", "0.5");
        hashMap.put("top_p", 1);
        hashMap.put("messages", arrayList);
        return JSONObject.toJSONString(hashMap);
    }


}
